package com.suao.web.controller.fruits;

import com.suao.common.annotation.Log;
import com.suao.common.constant.UserConstants;
import com.suao.common.core.controller.BaseController;
import com.suao.common.core.domain.AjaxResult;
import com.suao.common.core.page.TableDataInfo;
import com.suao.common.enums.BusinessType;
import com.suao.common.utils.StringUtils;
import com.suao.common.utils.poi.ExcelUtil;
import com.suao.fruits.domain.FruitsInfo;
import com.suao.fruits.service.IFruitsInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 水果信息管理Controller
 *
 * @author tyz
 * @date 2023-02-27
 */
@RestController
@RequestMapping("/fruits/fruitsInfo")
public class FruitsInfoController extends BaseController
{
    @Autowired
    private IFruitsInfoService fruitsInfoService;

    /**
     * 查询水果信息管理列表
     */
    @PreAuthorize("@ss.hasPermi('fruits:fruitsInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(FruitsInfo fruitsInfo)
    {
        startPage();
        List<FruitsInfo> list = fruitsInfoService.selectFruitsInfoList(fruitsInfo);
        return getDataTable(list);
    }

    /**
     * 导出水果信息管理列表
     */
    @PreAuthorize("@ss.hasPermi('fruits:fruitsInfo:export')")
    @Log(title = "水果信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FruitsInfo fruitsInfo)
    {
        List<FruitsInfo> list = fruitsInfoService.selectFruitsInfoList(fruitsInfo);
        ExcelUtil<FruitsInfo> util = new ExcelUtil<FruitsInfo>(FruitsInfo.class);
        util.exportExcel(response, list, "水果信息管理数据");
    }

    /**
     * 获取水果信息管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('fruits:fruitsInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(fruitsInfoService.selectFruitsInfoById(id));
    }

    /**
     * 新增水果信息管理
     */
    @PreAuthorize("@ss.hasPermi('fruits:fruitsInfo:add')")
    @Log(title = "水果信息管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FruitsInfo fruitsInfo)
    {
        if (StringUtils.isNotEmpty(fruitsInfo.getFruitsName())
                        && UserConstants.NOT_UNIQUE.equals(fruitsInfoService.checkFruitsNameUnique(fruitsInfo)))
        {
            return error("新增水果'" + fruitsInfo.getFruitsName() + "'失败，水果已存在");
        }
        return toAjax(fruitsInfoService.insertFruitsInfo(fruitsInfo));
    }

    /**
     * 修改水果信息管理
     */
    @PreAuthorize("@ss.hasPermi('fruits:fruitsInfo:edit')")
    @Log(title = "水果信息管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FruitsInfo fruitsInfo)
    {
        return toAjax(fruitsInfoService.updateFruitsInfo(fruitsInfo));
    }

    /**
     * 删除水果信息管理
     */
    @PreAuthorize("@ss.hasPermi('fruits:fruitsInfo:remove')")
    @Log(title = "水果信息管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(fruitsInfoService.deleteFruitsInfoByIds(ids));
    }
    
    /**
     * 查询水果信息管理列表
     */
    @GetMapping("/listAll")
    public TableDataInfo listAll(FruitsInfo fruitsInfo)
    {
        List<FruitsInfo> list = fruitsInfoService.selectFruitsInfoList(fruitsInfo);
        return getDataTable(list);
    }
}
